/* Copyright (c) 2004-2024 peigen.info. All rights reserved. */

package info.peigen.hotpot.manage.core.manage.controller;

import cn.hutool.core.collection.CollUtil;
import info.peigen.hotpot.common.core.result.manage.WebResult;
import info.peigen.hotpot.manage.core.manage.form.UserCreateForm;
import info.peigen.hotpot.manage.core.manage.form.UserEditForm;
import info.peigen.hotpot.manage.core.manage.form.UserSearchForm;
import info.peigen.hotpot.manage.core.manage.service.UserManageService;
import info.peigen.hotpot.manage.core.manage.service.export.ExportInfo;
import info.peigen.hotpot.manage.core.manage.service.export.ExportService;
import info.peigen.hotpot.manage.core.service.result.info.UserInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.noear.solon.annotation.Body;
import org.noear.solon.annotation.Controller;
import org.noear.solon.annotation.Inject;
import org.noear.solon.annotation.Mapping;
import org.noear.solon.validation.annotation.Valid;

import java.util.List;

/**
 * <b>(UserController)</b>
 * 管理后台 "用户"控制器
 *
 * @author Peigen
 * @version 1.0.0
 * @since 2024-08-02
 */
@SuppressWarnings({"rawtypes", "unused", "unchecked"})
@Api(tags = "用户")
@Valid
@Controller
@Mapping("/user/")
public class UserController {
    @Inject
    UserManageService userManageService;
    @Inject
    ExportService     exportService;

    @ApiOperation("分页查询")
    @Mapping("page")
    public WebResult<UserInfo> page(@Body UserSearchForm form) {
        return WebResult.convert(userManageService.page(form));
    }

    @ApiOperation("列表")
    @Mapping("list")
    public WebResult<UserInfo> list(@Body UserSearchForm form) {
        List<UserInfo> infos = userManageService.list(form);
        return WebResult.setSuccessResult().setInfo(infos);
    }

    @ApiOperation("新增")
    @Mapping("create")
    public WebResult<UserInfo> create(@Body UserCreateForm form) {
        UserInfo info = userManageService.create(form);
        return WebResult.setSuccessResult().setInfo(info);
    }

    @ApiOperation("修改")
    @Mapping("edit")
    public WebResult<UserInfo> edit(@Body UserEditForm form) {
        UserInfo info = userManageService.edit(form);
        return WebResult.setSuccessResult().setInfo(info);
    }

    @ApiOperation("删除")
    @Mapping("remove")
    public WebResult remove(Integer id) {
        userManageService.remove(id);
        return WebResult.setSuccessResult();
    }

    @ApiOperation("批量删除")
    @Mapping("removes")
    public WebResult removes(List<Integer> ids) {
        userManageService.removes(ids);
        return WebResult.setSuccessResult();
    }

    @ApiOperation("详情")
    @Mapping("detail")
    public WebResult<UserInfo> detail(Integer id) {
        UserInfo info = userManageService.detail(id);
        return WebResult.setSuccessResult().setInfo(info);
    }

    @ApiOperation("导出")
    @Mapping("export")
    public WebResult<ExportInfo> export(@Body UserSearchForm form) {
        List<UserInfo> infos = userManageService.list(form);
        if (CollUtil.isEmpty(infos)) {
            return WebResult.setSuccessResult();
        }
        ExportInfo info = exportService.export(infos, "用户-导出");
        return WebResult.setSuccessResult().setInfo(info);
    }
}
